import AESFun from '@/js_sdk/ar-aes/ar-aes.js'
var AES = AESFun.AES;
import chooseFile from './tools/chooseFile.js'
import compressImage from './tools/compressImage.js'
import uploadFile from './tools/uploadFile.js'
import base64 from './tools/base64.js'
import md5 from './tools/md5.js'
import addressParse from './tools/addressParse.js'

var $tools = {
	chooseFile,
	compressImage,
	uploadFile,
	// base64,
	// md5,
	// addressParse,
	AES,
	//延迟等待
	async sleep(time = 100, tag = '') {
		return new Promise((resolve, reject) => { setTimeout(() => { resolve(); }, time) })
	},
	// 时间戳推算多久之前
	beforeTime(dateTime = null) {
		// 如果为null,则格式化当前时间
		if (!dateTime) dateTime = Number(new Date());
		// 如果dateTime长度为10或者13，则为秒和毫秒的时间戳，如果超过13位，则为其他的时间格式
		if (dateTime.toString().length == 10) dateTime *= 1000;
		let timestamp = +new Date(Number(dateTime));
	
		let timer = (Number(new Date()) - timestamp) / 1000;
		// 如果小于5分钟,则返回"刚刚",其他以此类推
		let tips = '';
		switch (true) {
			case timer < 300:
				tips = '刚刚';
				break;
			case timer >= 300 && timer < 3600:
				tips = parseInt(timer / 60) + '分钟前';
				break;
			case timer >= 3600 && timer < 86400:
				tips = parseInt(timer / 3600) + '小时前';
				break;
			case timer >= 86400 && timer < 2592000:
				tips = parseInt(timer / 86400) + '天前';
				break;
			default:
				if (timer >= 2592000 && timer < 365 * 86400) {
					tips = parseInt(timer / (86400 * 30)) + '个月前';
				} else {
					tips = parseInt(timer / (86400 * 365)) + '年前';
				}
		}
		return tips;
	},
	formatNumber(n) {
		n = n.toString()
		return n[1] ? n : '0' + n
	},
	//时间戳转日期
	timeToDate(number, format = 'h:m:s M/D/Y') { //时间搓格式化
		if (!number || number == 0) return '';
		var formateArr = ['Y', 'M', 'D', 'h', 'm', 's'];
		var returnArr = [];
		var date = new Date(number * 1000);
		returnArr.push(date.getFullYear());
		returnArr.push($tools.formatNumber(date.getMonth() + 1));
		returnArr.push($tools.formatNumber(date.getDate()));
		returnArr.push($tools.formatNumber(date.getHours()));
		returnArr.push($tools.formatNumber(date.getMinutes()));
		returnArr.push($tools.formatNumber(date.getSeconds()));
		for (var i in returnArr) {
			format = format.replace(formateArr[i], returnArr[i]);
		}
		// console.log(format);
		return format;
	},
	countDownDate(start, end) {
		let str = '';
		if (start > end) return str;
		let diff = end - start;
		let d = parseInt(diff / 86400);
		if (d > 0) {
			str += d + ':';
			diff -= d * 86400;
		}
		let h = parseInt(diff / 3600);
		if (h >= 0) {
			h = h < 10 ? '0' + h : h;
			str += h + ':';
			diff -= h * 3600;
		}
		let m = parseInt(diff / 60);
		if (m >= 0) {
			m = m < 10 ? '0' + m : m;
			str += m + ':';
			diff -= m * 60;
		}
		let s = diff;
		if (s >= 0) {
			s = s < 10 ? '0' + s : s;
			str += s;
		}
		return str;
	},
	formatMoney(num) {
		if (!num) return '0.00';
		num = num.toString().replace(/[^\d\.-]/g, ''); //转成字符串并去掉其中除数字, . 和 - 之外的其它字符。
		if (isNaN(num)) num = "0"; //是否非数字值
		var sign = (num == (num = Math.abs(num)));
		num = Math.floor(num * 100 + 0.50000000001); //下舍入
		var cents = num % 100; //求余 余数 = 被除数 - 除数 * 商
		cents = (cents < 10) ? "0" + cents : cents; //小于2位数就补齐
		num = Math.floor(num / 100).toString();
		for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) { //每隔三位小数分始开隔
			//4 ==> 三位小数加一个分隔符，
			num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
		}
		return (((sign) ? '' : '-') + num + '.' + cents);
	},
	getListVal(list, key, keyName = 'key', valueName = 'val') {
		for (let i in list) {
			let row = list[i];
			if(typeof row == 'string'){
				if(key == i) return row;
			}else{
				if (row[keyName] == key) return row[valueName];
			}
			
		}
		return '';
	},
	getUrlParamsToString(obj) {
		let params = '';
		for (let key in obj) {
			let val = obj[key];
			params += params ? '&' : '?';
			params += key + "=" + val;
		}
		return params;
	},
	// 递归
	cartesianProductOf(e) {
		return Array.prototype.reduce.call(arguments, function(a, b) {
			var ret = [];
			a.forEach(function(a) {
				b.forEach(function(b) {
					if (b.value != '' && b != '') {
						ret.push(a.concat([b.value]));
					}
				});
			});
			return ret;
		}, [
			[]
		]);
	},
};


export default $tools;